package com.example.back.mapper;

import com.example.back.entity.Notice;
import com.example.back.util.StringUtil;

public class NoticeMapperProvider {
    public String getSelectNoticeSql(Notice notice) {
        StringBuffer sql = new StringBuffer("select id,userId,time,text,url,type,triggerUserId,postId,isRead from notice where 1=1 ");
        if (StringUtil.isNotEmpty(notice.getText())) {
            sql.append(" and text like concat('%',#{text},'%')");
        }
        if (StringUtil.isNotEmpty(notice.getTime())) {
            sql.append(" and time like concat('%',#{time},'%')");
        }
        if (StringUtil.isNotEmpty(notice.getUrl())){
            sql.append(" and url like concat('%',#{url},'%')");
        }
        if (StringUtil.isNotEmpty(notice.getType())){
            sql.append(" and type = #{type}");
        }
        if(notice.getUserId() != null){
            sql.append(" and userId = #{userId}");
        }
        if(notice.getTriggerUserId() != null){
            sql.append(" and triggerUserId = #{triggerUserId}");
        }
        if(notice.getPostId() != null){
            sql.append(" and postId = #{postId}");
        }
        return sql.toString();
    }

    public String getUpdateNoticeSql(Notice notice) {
        StringBuffer sql = new StringBuffer("update notice set id = #{id} ");
        if (StringUtil.isNotEmpty(notice.getText())) {
            sql.append(" ,text = #{text} ");
        }
        if (StringUtil.isNotEmpty(notice.getTime())) {
            sql.append(" ,time = #{time} ");
        }
        if (StringUtil.isNotEmpty(notice.getUrl())) {
            sql.append(" ,url = #{url} ");
        }
        if (StringUtil.isNotEmpty(notice.getType())) {
            sql.append(" ,type = #{type} ");
        }
        if(notice.getUserId() != null){
            sql.append(" ,userId = #{userId} ");
        }
        if(notice.getTriggerUserId() != null){
            sql.append(" ,triggerUserId = #{triggerUserId} ");
        }
        if(notice.getPostId() != null){
            sql.append(" ,postId = #{postId} ");
        }
        sql.append(" where id = #{id}");
        return sql.toString();
    }
}